package com.shou.trigger.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.api.model.dto.DeleteDTO;
import com.shou.api.model.dto.PageQueryDTO;
import com.shou.api.model.dto.FeeScheduleDTO;
import com.shou.api.model.response.ResultData;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.fee.model.FeeScheduleEntity;
import com.shou.domain.fee.service.IFeeScheduleService;
import com.shou.trigger.http.mapstruct.FeeScheduleDTOConvert;
import com.shou.trigger.http.mapstruct.common.PageQueryDTOConvert;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName:FeescheduleController
 * Project:SmartPark
 * Package: com.shou.trigger.http
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/9 18:22
 * @Version 1.0
 */
@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/fee-schedule")
public class FeeScheduleController extends AbstructController {
    @Resource
    private IFeeScheduleService feeScheduleService;
    @Resource
    private PageQueryDTOConvert pageQueryDTOConvert;
    @Resource
    private FeeScheduleDTOConvert feeScheduleDTOConvert;

    /**
     * 分页查询
     */
    @GetMapping("/fee-schedule-info")
    public ResultData<IPage<FeeScheduleDTO>> page(@RequestParam Map<String, Object> rulePageQueryDTOMap){
        PageQueryDTO pageQueryDTO = PageQueryDTO.getRulePageQueryDTOFromMap(rulePageQueryDTOMap);
        // 入参校验
        validateQueryParams(pageQueryDTO);
        // 将DTO对象转换为实体类
        PageQueryEntity pageQueryEntity = pageQueryDTOConvert.convertPageQueryDTOToEntity(pageQueryDTO);
        // 分页查询
        IPage<FeeScheduleEntity> queueMessageList = feeScheduleService.pageQuery(pageQueryEntity);
        return ResultData.success(feeScheduleDTOConvert.convertFeeScheduleEntityPageToDTO(queueMessageList));
    }

    /**
     * 新增收费标准信息
     */
    @PostMapping("/fee-schedule-info")
    public ResultData<String> insertFeeSchedule(@RequestBody FeeScheduleDTO parkingSpaceDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        FeeScheduleEntity feeScheduleEntity = feeScheduleDTOConvert.convertFeeScheduleDTOToEntity(parkingSpaceDTO);
        // 保存(新增)规则
        feeScheduleService.insertFeeSchedule(feeScheduleEntity);
        return ResultData.success("insertFeeSchedule success");
    }

    /**
     * 删除单/多个收费标准信息
     */
    @DeleteMapping("/fee-schedule-info")
    public ResultData<String> deleteFeeScheduleByIds(@RequestBody @Validated DeleteDTO deleteDTO) {
        // 根据传递的ID(可以是多个)删除收费标准信息
        feeScheduleService.deleteByIds(deleteDTO.getIds());
        return ResultData.success("deleteFeeScheduleByIds success");
    }

    /**
     * 清空所有收费标准信息
     */
    @DeleteMapping("/all-fee-schedule-info")
    public ResultData<String> deleteAll() {
        // 清空收费标准信息表
        feeScheduleService.deleteAll();
        return ResultData.success("deleteAll success");
    }

    /**
     * 根据收费标准ID更新收费标准信息
     */
    @PutMapping("/fee-schedule-info")
    public ResultData<String> updateFeeScheduleById(@RequestBody @Validated FeeScheduleDTO rule) {
        // 将DTO对象转换为Entity对象，调用domain层方法
        FeeScheduleEntity feeScheduleEntity = feeScheduleDTOConvert.convertFeeScheduleDTOToEntity(rule);
        // 根据ID更新收费标准信息
        feeScheduleService.updateFeeScheduleById(feeScheduleEntity);
        return ResultData.success("updateFeeScheduleById success");
    }
}
